import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:xianyu_school/utils/url.dart';
import 'package:xianyu_school/xianyuApp/entity/goods/goods.dart';
import 'package:xianyu_school/xianyuApp/home/List/homeGoodItem.dart';

import 'Widgets/BallPulseIndicator.dart';

class myBroseHisPage extends StatelessWidget{

  List<goods> gs = [];
  String url = urls().url;

  Future<void> getgoods() async {
    try{
      SharedPreferences pref = await SharedPreferences.getInstance();
      String username = pref.getString("username");
      Response re = await Dio().get(url+"/users/getbrosehis",queryParameters: {"username":username});
      return re;
    }
    catch(e){
      return ;
    }
  }


  Widget build(BuildContext context){
    return Scaffold(
      appBar: CupertinoNavigationBar(
        backgroundColor: Colors.yellow,
        middle: Text('浏览历史'),
      ),
      body: FutureBuilder(
        future: getgoods(),
        builder: (context,snapshot){
          if(snapshot.hasData){
            if(snapshot.data.toString()=="No more data"){
              Fluttertoast.showToast(
                  msg: "暂无数据",
                  toastLength: Toast.LENGTH_SHORT,
                  gravity: ToastGravity.BOTTOM,
                  timeInSecForIosWeb: 1,
                  backgroundColor: Colors.black45,
                  textColor: Colors.white,
                  fontSize: 16.0
              );
              return Text(" ");
            }
            else{
              var responseJson = json.decode(snapshot.data.toString());
              responseJson.forEach((data) {
                gs.add(goods.fromJson(data));
              });
              return CustomScrollView(
                slivers: <Widget>[
                  // 顶部偏移量
//                    SliverOverlapInjector(
//                      handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
//                    ),

                  // 商品列表
                  SliverPadding(
                    padding: EdgeInsets.fromLTRB(10, 5, 10, 10),
                    sliver: SliverStaggeredGrid.countBuilder(

                      crossAxisCount: 1,
                      crossAxisSpacing: 10,
                      mainAxisSpacing: 10,
                      itemCount: gs.length,
                      itemBuilder: (BuildContext context, int index) {
                        return FHHomeGoodItem(model: gs[index],);
                      },
                      staggeredTileBuilder: (int index) {
                        return StaggeredTile.fit(2);
                      },
                    ),
                  ),

                  // 刷新控件
                ],
              );
            }
          }
          else return Center(
          child: BallPulseIndicator(
          color: Colors.white
            ),
          );
        },
      ),
    );
  }
}